package top.llove.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.llove.mapper.KindMapper;
import top.llove.pojo.Kind;
import top.llove.service.KindService;
import top.llove.utils.res.ApiResult;

import java.util.List;

/**
 * 文章分类Impl
 *
 * @author: 幸会
 * @date: 2021/6/22 14:43
 * @email: 1846724873@qq.com
 * @description: 文章分类Impl
 * version: 1.0
 */
@Slf4j
@Service
public class KindServiceImpl implements KindService {
    @Autowired
    private KindMapper kind; // 文章分类Mapper

    @Autowired
    private ApiResult res; // 返回结果

    @Override
    public ApiResult save(String name, String user) {
        boolean ok = false;
        try {
            ok = kind.save(name, user) == 1;
            log.debug("添加文章分类" + (ok ? "成功" : "失败") + ", 分类名称: " + name + ", 分类所属管理员ID: "+ user);
            res = ApiResult.success(200, "添加文章分类" + (ok ? "成功" : "失败"), ok);
        } catch (Exception e) {
            e.printStackTrace();
            res = ApiResult.fail(500, e.getMessage(), ok);
        }
        return res;
    }

    @Override
    public ApiResult query(String user) {
        try {
            List<Kind> kinds = kind.query(user);
            log.debug("文章分类查询成功, 数据为: " + kinds);
            Object data = kinds.size() > 0 ? kinds : "暂无数据";
            res =ApiResult.success(200, "查询成功", data);
        } catch (Exception e) {
            e.printStackTrace();
            res = ApiResult.fail(500, e.getMessage(), false);
        }
        return res;
    }

    @Override
    public ApiResult delete(String id, String user) {
        boolean ok = false;
        try {
            ok = kind.delete(id, user) > 0;
            res = ApiResult.success(200, "删除" + (ok ? "成功" : "失败"), ok);
        } catch (Exception e) {
            e.printStackTrace();
            ok = false;
            res = ApiResult.fail(500, e.getMessage(), ok);
        }
        return res;
    }
}
